package com.hpin.fansea.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hpin.fansea.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

import java.util.List;

/**
 * Created by Aaron on 15/10/21.
 */
public class PictureAdapter extends PagerAdapter {

    private Context ctx;
    private List<String> picsList;
    public DisplayImageOptions options;

    public PictureAdapter(List<String> picsList, Context ctx) {

        this.ctx = ctx;
        this.picsList = picsList;

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.icon_banner_loading)
                .showImageForEmptyUri(R.mipmap.icon_banner_fail)
                .showImageOnFail(R.mipmap.icon_banner_fail)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true).cacheInMemory(true)
                .cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new SimpleBitmapDisplayer()).build();

    }

//    public void setPicsList(List<BannerResult.BannerDetail> picsList) {
//        this.picsList = picsList;
//    }

    @Override
    public int getCount() {
        return picsList.size();
//        return Constant.MAX_SIZE;
    }

    /**
     * 实例化指定位置的view
     */
    public Object instantiateItem(ViewGroup container, final int position) {

//        String banner = ;
        ImageView view = new ImageView(ctx);
        if (picsList==null || picsList.size()==0) {
            //没有轮播图
            return view;
        }
//        bitmapUtils.display(view, banner);

//        LogUtil.e("tag", "position===" + position + ",picsList.size()===" + picsList.size());

        ImageLoader.getInstance().displayImage(picsList.get(position), view, options);

        view.setScaleType(ImageView.ScaleType.CENTER_CROP);
        container.addView(view);

//        view.setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//
////                if (picsList != null && picsList.get(position) != null) {
//
////                    String detail = picsList.get(position).detail;
//
////                    //有groupbuy则调整到图团购页面
////                    if (Constant.JUMP_PURCHASE.equals(picsList.get(position).module)) {
////                        Intent purchaseIntent = new Intent(ctx, PurchaseGfDetailActivity.class);
////                        purchaseIntent.putExtra("moduleId", picsList.get(position).moduleId);
////                        ctx.startActivity(purchaseIntent);
////
////                        return;
////                    }
//
////                    //有详情 跳转到详情页面
////                    if (!TextUtils.isEmpty(detail)) {
////                        Intent intent = new Intent(ctx, ChangePicsDetailActivity.class);
////                        intent.putExtra("detail", detail);
////                        ctx.startActivity(intent);
////                    }
//
////                }
//
//            }
//        });

        return view;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

}
